from app import create_app, db
from app.models.airport import Airport
from app.models.runway import Runway
from app.models.parking_apron import ParkingApron
from app.models.oil_tank import OilTank
from app.models.ammunition_depot import AmmunitionDepot
from app.models.aircraft import Aircraft

def init_db():
    app = create_app()
    with app.app_context():
        # 删除所有表
        db.drop_all()
        # 创建所有表
        db.create_all()
        
        # 添加示例数据
        # 1. 创建机场
        airport = Airport(
            name="示例机场",
            location="示例位置",
            capacity=100,
            hangar_capacity=20,
            status="active"
        )
        db.session.add(airport)
        db.session.commit()
        
        # 2. 创建跑道
        runway = Runway(
            airport_id=airport.id,
            name="主跑道",
            length=3000,
            width=45,
            surface_type="concrete",
            status="active"
        )
        db.session.add(runway)
        
        # 3. 创建停机坪
        apron = ParkingApron(
            airport_id=airport.id,
            name="主停机坪",
            capacity=50,
            surface_area=10000,
            status="active"
        )
        db.session.add(apron)
        
        # 4. 创建油库
        oil_tank = OilTank(
            airport_id=airport.id,
            name="主油库",
            capacity=1000000,
            current_volume=500000,
            fuel_type="aviation",
            status="active"
        )
        db.session.add(oil_tank)
        
        # 5. 创建弹药库
        ammo_depot = AmmunitionDepot(
            airport_id=airport.id,
            name="主弹药库",
            capacity=1000,
            current_stock=500,
            security_level="high",
            status="active"
        )
        db.session.add(ammo_depot)
        
        # 6. 创建飞机
        aircraft = Aircraft(
            airport_id=airport.id,
            name="战斗机-001",
            type="fighter",
            status="active",
            maintenance_status="good"
        )
        db.session.add(aircraft)
        
        # 提交所有更改
        db.session.commit()
        
        print("数据库初始化完成！")

if __name__ == "__main__":
    init_db() 